/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package chameleon;



import chameleon.model.formula.Formula;
import chameleon.model.formula.parser.BooleanParser;
import chameleon.model.formula.parser.LtlParser;
import chameleon.model.formula.parser.MathematicalParser;
import chameleon.view.input.Input;
import chameleon.view.InputManager;
import chameleon.view.OutputManager;
import chameleon.view.command.Command;
import chameleon.view.command.CommandFactory;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;





/**
 * This class is the main routine to Chameleon. Main starts with no arguments.
 * After user give the command name and his options. While the command is different
 * to quit command, main create the command, via the @view.command.CommandFactory.
 * After the creation of the command, main executes the command.
 * @author simon
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException{ 
        String strCmd;
        Command cmd;
        InputManager inputManager = new InputManager();
        OutputManager outputManager = new OutputManager();
        Input input = new Input();
        

        outputManager.displayString("Welcome in Chameleon\n>\n"
                + "WARNING : don't use capital letter for variables names in LTL formula\n\n");
        boolean quit = false;
        
        if (args.length != 0){
            //We have a test file
            InputStream ips=new FileInputStream(args[0]); 
            InputStreamReader ipsr=new InputStreamReader(ips);
            BufferedReader br=new BufferedReader(ipsr);
            while (((strCmd=br.readLine())!=null) && !quit ){
                    outputManager.displayString(strCmd+"\n");
                    cmd  = CommandFactory.createCommand(input, strCmd);
                    quit = cmd.quit();
                    Input saveInput = input;
                    if ((input=cmd.execute())==null){
                        input = saveInput;
                    }
                    outputManager.displayString("done...\n>");
            }
            br.close(); 
        } 
        
        if (!quit){

        strCmd = inputManager.readInputString();
        cmd  = CommandFactory.createCommand(input, strCmd);
        while(!cmd.quit()){

            Input saveInput = input;
            if ((input=cmd.execute())==null){
                input = saveInput;
            }
            outputManager.displayString("done...\n>");
            strCmd = inputManager.readInputString();
            cmd  = CommandFactory.createCommand(input, strCmd);
        }
        /**/
        }
    }
}
